/**
 * Created by Administrator on 2016/8/1.
 */
var BaseDao = require('../../base-dao/BaseDao');
var async = require('async');

//确认订单页面
exports.comfirmorder = function(userid,callback) {
    var sql="select * from tbl_cart  where userid = '"+userid+"' and status=2";
    BaseDao.execQuery(sql, function(err, rows) {
            callback(err, rows);
    });
};

//确认订单定金待支付数据
exports.advancewait = function(objs,cb) {
    var sql = 'INSERT INTO tbl_perorder SET ?';
    var loop = 0;
    async.whilst(
        function() {
            return loop<objs.length;
        },
        function(wcb) {
            BaseDao.execInsert(sql, objs[loop], function(err, doc) {
                loop++;
                wcb(err);
            })
        },
        function(err) {
            cb(err, {
                data: objs
            })
        }
    )
};

//确认订单清除购物车
exports.clearcart = function(objs,cb) {
    var loop = 0;
    async.whilst(
        function() {
            return loop<objs.length;
        },
        function(wcb) {
            var sql = 'update tbl_cart set status=0 where id="'+objs[loop]+'"';
            BaseDao.execInsert(sql, objs[loop], function(err, doc) {
                loop++;
                wcb(err);
            })
        },
        function(err) {
            cb(err, {
                data: objs
            })
        }
    )
};

//确认订单已支付定金
exports.payearnest = function(userid,orderno,source,deposit,sid,callback) {
    var sql;
    if(source=='wx'){
         sql=" update tbl_perorder set orderstatus=3, source = 'wx' where userid = '"+userid+"' and orderno = '"+orderno+"' and status=1 ";
    }
    if(source=='ali'){
         sql=" update tbl_perorder set orderstatus=3, source= 'api' where userid = '"+userid+"' and orderno = '"+orderno+"' and status=1 ";
    }
    BaseDao.execQuery(sql, function(err, rows1) {
        callback(err, rows1);
    });
};

//增加pay表
exports.addpay = function(obj,callback) {
    var sql = 'INSERT INTO tbl_pay SET ?';
    BaseDao.execQuery(sql,obj, function(err, rows2) {
        callback(err, rows2);
    });
};

//我的购物车
exports.shoppingcart = function(userid,callback) {
    var sql="select c.* from tbl_cart c left join tbl_goods g on c.type = g.type where userid = '"+userid+"' and c.status=1 and g.status=1 order by type desc";
    BaseDao.execQuery(sql, function(err, rows) {
        callback(err, rows);
    });
};

